package net.lcheng.commons.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 产生流水号工具类
 * Created by qiliping on 15/12/15.
 */
public class SerialNum {
    private static String count = "000";
    private static String dateValue = "20151111";
    /**
     * * 产生流水号
    */
    public synchronized static String getOrderNo(String prefix) {
        long No = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        String nowdate = sdf.format(new Date());
        No = Long.parseLong(nowdate);
        if (!(String.valueOf(No)).equals(dateValue)) {
            count = "000";
            dateValue = String.valueOf(No);
        }
        String num = String.valueOf(No);
        num += getNo(count);
        num = prefix + num;
        return num;
    }
    /**
     * * 返回当天的订单数+1
     */
    public static String getNo(String s) {
        String rs = s;
        int i = Integer.parseInt(rs);
        i += 1;
        rs = "" + i;
        for (int j = rs.length(); j < 3; j++) {
            rs = "0" + rs;
        }
        count = rs;
        return rs;
    }
}
